<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>照片上传与展示</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
        }
        
        header {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            padding: 2rem 0;
            text-align: center;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 0.5rem;
        }
        
        .subtitle {
            font-size: 1.1rem;
            opacity: 0.9;
            font-weight: 300;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
        }
        
        .upload-section {
            background: white;
            border-radius: 12px;
            padding: 2rem;
            margin-bottom: 2rem;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            text-align: center;
        }
        
        .upload-area {
            border: 2px dashed #6a11cb;
            border-radius: 8px;
            padding: 2rem;
            margin: 1rem 0;
            cursor: pointer;
            transition: all 0.3s ease;
            background-color: #f8f9ff;
        }
        
        .upload-area:hover {
            background-color: #eef1ff;
            border-color: #2575fc;
        }
        
        .upload-area.highlight {
            background-color: #e6f0ff;
            border-color: #2575fc;
        }
        
        .upload-icon {
            font-size: 3rem;
            color: #6a11cb;
            margin-bottom: 1rem;
        }
        
        .upload-text {
            margin-bottom: 1rem;
        }
        
        .upload-btn {
            background: #6a11cb;
            color: white;
            border: none;
            padding: 0.8rem 1.5rem;
            border-radius: 30px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        
        .upload-btn:hover {
            background: #2575fc;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
        }
        
        .file-input {
            display: none;
        }
        
        .filter-bar {
            display: flex;
            justify-content: center;
            margin-bottom: 2rem;
            flex-wrap: wrap;
            gap: 10px;
        }
        
        .filter-btn {
            background: white;
            border: none;
            padding: 0.7rem 1.5rem;
            border-radius: 30px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        
        .filter-btn:hover {
            background: #6a11cb;
            color: white;
            transform: translateY(-2px);
        }
        
        .filter-btn.active {
            background: #6a11cb;
            color: white;
        }
        
        .photo-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 20px;
        }
        
        .photo-item {
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            background: white;
            cursor: pointer;
            position: relative;
        }
        
        .photo-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
        }
        
        .photo-img {
            width: 100%;
            height: 220px;
            object-fit: cover;
            display: block;
        }
        
        .photo-info {
            padding: 1rem;
        }
        
        .photo-title {
            font-weight: 600;
            margin-bottom: 0.5rem;
            color: #333;
        }
        
        .photo-date {
            font-size: 0.85rem;
            color: #777;
        }
        
        .delete-btn {
            position: absolute;
            top: 10px;
            right: 10px;
            background: rgba(255, 0, 0, 0.7);
            color: white;
            border: none;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            opacity: 0;
            transition: opacity 0.3s;
        }
        
        .photo-item:hover .delete-btn {
            opacity: 1;
        }
        
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.9);
            z-index: 1000;
            justify-content: center;
            align-items: center;
            padding: 2rem;
        }
        
        .modal-content {
            max-width: 90%;
            max-height: 90%;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 5px 30px rgba(0, 0, 0, 0.3);
        }
        
        .modal-img {
            width: 100%;
            height: auto;
            display: block;
        }
        
        .close-btn {
            position: absolute;
            top: 20px;
            right: 30px;
            color: white;
            font-size: 2.5rem;
            cursor: pointer;
            transition: color 0.3s;
        }
        
        .close-btn:hover {
            color: #6a11cb;
        }
        
        .nav-btn {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: none;
            font-size: 2rem;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            cursor: pointer;
            transition: background 0.3s;
        }
        
        .nav-btn:hover {
            background: rgba(255, 255, 255, 0.4);
        }
        
        .prev-btn {
            left: 30px;
        }
        
        .next-btn {
            right: 30px;
        }
        
        footer {
            text-align: center;
            padding: 2rem;
            margin-top: 2rem;
            background: #2c3e50;
            color: white;
        }
        
        @media (max-width: 768px) {
            .photo-grid {
                grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
            }
            
            h1 {
                font-size: 2rem;
            }
            
            .nav-btn {
                width: 40px;
                height: 40px;
                font-size: 1.5rem;
            }
        }
        
        @media (max-width: 480px) {
            .photo-grid {
                grid-template-columns: 1fr;
            }
            
            .filter-bar {
                flex-direction: column;
                align-items: center;
            }
            
            .filter-btn {
                width: 80%;
            }
        }
    </style>
</head>
<body>
    <header>
        <h1>照片上传与展示</h1>
        <p class="subtitle">分享您的精彩瞬间，珍藏美好回忆</p>
    </header>
    
    <div class="container">
        <div class="upload-section">
            <h2>上传照片</h2>
            <p class="upload-text">支持JPG、PNG格式，单张图片不超过5MB</p>
            
            <div class="upload-area" id="uploadArea">
                <div class="upload-icon">📁</div>
                <p>拖放照片到这里，或点击选择文件</p>
                <input type="file" id="fileInput" class="file-input" accept="image/*" multiple>
            </div>
            
            <button class="upload-btn" id="uploadBtn">选择文件</button>
        </div>
        
        <div class="filter-bar">
            <button class="filter-btn active" data-filter="all">全部照片</button>
            <button class="filter-btn" data-filter="nature">自然风光</button>
            <button class="filter-btn" data-filter="portrait">人物肖像</button>
            <button class="filter-btn" data-filter="urban">城市建筑</button>
            <button class="filter-btn" data-filter="travel">旅行记录</button>
        </div>
        
        <div class="photo-grid" id="photoGrid">
            <!-- 照片将通过JavaScript动态添加 -->
        </div>
    </div>
    
    <div class="modal" id="imageModal">
        <span class="close-btn" id="closeModal">&times;</span>
        <button class="nav-btn prev-btn" id="prevBtn">&#10094;</button>
        <button class="nav-btn next-btn" id="nextBtn">&#10095;</button>
        <div class="modal-content">
            <img class="modal-img" id="modalImage" src="" alt="预览图片">
        </div>
    </div>
    
    <footer>
        <p>&copy; 2023 照片上传与展示 | 用镜头记录生活</p>
    </footer>
    
    <script>
        // 照片数据 - 初始示例照片
        let photos = [
            { id: 1, src: 'https://picsum.photos/id/10/800/600', title: '山间溪流', date: '2023-05-15', category: 'nature' },
            { id: 2, src: 'https://picsum.photos/id/11/800/600', title: '城市夜景', date: '2023-06-22', category: 'urban' },
            { id: 3, src: 'https://picsum.photos/id/12/800/600', title: '海滩日落', date: '2023-07-10', category: 'travel' },
            { id: 4, src: 'https://picsum.photos/id/13/800/600', title: '森林小径', date: '2023-04-05', category: 'nature' },
            { id: 5, src: 'https://picsum.photos/id/14/800/600', title: '现代建筑', date: '2023-08-18', category: 'urban' },
            { id: 6, src: 'https://picsum.photos/id/15/800/600', title: '人像摄影', date: '2023-09-02', category: 'portrait' }
        ];
        
        // DOM元素
        const photoGrid = document.getElementById('photoGrid');
        const filterBtns = document.querySelectorAll('.filter-btn');
        const modal = document.getElementById('imageModal');
        const modalImage = document.getElementById('modalImage');
        const closeModal = document.getElementById('closeModal');
        const prevBtn = document.getElementById('prevBtn');
        const nextBtn = document.getElementById('nextBtn');
        const uploadArea = document.getElementById('uploadArea');
        const fileInput = document.getElementById('fileInput');
        const uploadBtn = document.getElementById('uploadBtn');
        
        let currentFilter = 'all';
        let currentImageIndex = 0;
        let currentFilteredPhotos = [];
        
        // 初始化照片网格
        function initPhotoGrid() {
            photoGrid.innerHTML = '';
            currentFilteredPhotos = currentFilter === 'all' 
                ? photos 
                : photos.filter(photo => photo.category === currentFilter);
            
            currentFilteredPhotos.forEach((photo, index) => {
                const photoElement = document.createElement('div');
                photoElement.className = 'photo-item';
                photoElement.setAttribute('data-category', photo.category);
                photoElement.innerHTML = `
                    <img src="${photo.src}" alt="${photo.title}" class="photo-img">
                    <div class="photo-info">
                        <h3 class="photo-title">${photo.title}</h3>
                        <p class="photo-date">${photo.date}</p>
                    </div>
                    <button class="delete-btn" data-id="${photo.id}">×</button>
                `;
                
                photoElement.addEventListener('click', (e) => {
                    if (!e.target.classList.contains('delete-btn')) {
                        openModal(index, currentFilteredPhotos);
                    }
                });
                
                // 删除按钮事件
                const deleteBtn = photoElement.querySelector('.delete-btn');
                deleteBtn.addEventListener('click', (e) => {
                    e.stopPropagation();
                    deletePhoto(photo.id);
                });
                
                photoGrid.appendChild(photoElement);
            });
        }
        
        // 删除照片
        function deletePhoto(id) {
            if (confirm('确定要删除这张照片吗？')) {
                photos = photos.filter(photo => photo.id !== id);
                initPhotoGrid();
            }
        }
        
        // 打开模态框显示大图
        function openModal(index, photoArray) {
            currentImageIndex = index;
            modalImage.src = photoArray[index].src;
            modal.style.display = 'flex';
            document.body.style.overflow = 'hidden';
        }
        
        // 关闭模态框
        function closeModalFunc() {
            modal.style.display = 'none';
            document.body.style.overflow = 'auto';
        }
        
        // 显示下一张图片
        function showNextImage() {
            currentImageIndex = (currentImageIndex + 1) % currentFilteredPhotos.length;
            modalImage.src = currentFilteredPhotos[currentImageIndex].src;
        }
        
        // 显示上一张图片
        function showPrevImage() {
            currentImageIndex = (currentImageIndex - 1 + currentFilteredPhotos.length) % currentFilteredPhotos.length;
            modalImage.src = currentFilteredPhotos[currentImageIndex].src;
        }
        
        // 处理文件上传
        function handleFileUpload(files) {
            for (let i = 0; i < files.length; i++) {
                const file = files[i];
                
                // 检查文件类型
                if (!file.type.match('image.*')) {
                    alert('请上传图片文件！');
                    continue;
                }
                
                // 检查文件大小（5MB限制）
                if (file.size > 5 * 1024 * 1024) {
                    alert('文件大小不能超过5MB！');
                    continue;
                }
                
                // 创建文件URL
                const fileURL = URL.createObjectURL(file);
                
                // 创建新的照片对象
                const newPhoto = {
                    id: Date.now() + i, // 使用时间戳作为ID
                    src: fileURL,
                    title: file.name.split('.')[0], // 使用文件名作为标题
                    date: new Date().toISOString().split('T')[0], // 当前日期
                    category: 'nature' // 默认分类
                };
                
                // 添加到照片数组
                photos.unshift(newPhoto);
            }
            
            // 重新初始化照片网格
            initPhotoGrid();
            
            // 重置文件输入
            fileInput.value = '';
        }
        
        // 事件监听
        filterBtns.forEach(btn => {
            btn.addEventListener('click', () => {
                // 更新活动按钮
                filterBtns.forEach(b => b.classList.remove('active'));
                btn.classList.add('active');
                
                // 更新过滤器并重新渲染网格
                currentFilter = btn.getAttribute('data-filter');
                initPhotoGrid();
            });
        });
        
        closeModal.addEventListener('click', closeModalFunc);
        
        // 点击模态框背景关闭
        modal.addEventListener('click', (e) => {
            if (e.target === modal) {
                closeModalFunc();
            }
        });
        
        // 键盘导航
        document.addEventListener('keydown', (e) => {
            if (modal.style.display === 'flex') {
                if (e.key === 'Escape') closeModalFunc();
                if (e.key === 'ArrowRight') showNextImage();
                if (e.key === 'ArrowLeft') showPrevImage();
            }
        });
        
        // 为导航按钮添加事件监听
        prevBtn.addEventListener('click', showPrevImage);
        nextBtn.addEventListener('click', showNextImage);
        
        // 上传按钮点击事件
        uploadBtn.addEventListener('click', () => {
            fileInput.click();
        });
        
        // 文件选择变化事件
        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                handleFileUpload(e.target.files);
            }
        });
        
        // 拖放事件
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.classList.add('highlight');
        });
        
        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('highlight');
        });
        
        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('highlight');
            
            if (e.dataTransfer.files.length > 0) {
                handleFileUpload(e.dataTransfer.files);
            }
        });
        
        // 点击上传区域触发文件选择
        uploadArea.addEventListener('click', () => {
            fileInput.click();
        });
        
        // 初始化
        initPhotoGrid();
    </script>
</body>
</html>